
package com.edgar.webviewhtml;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.edgar.main.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    private WebView wvDetailBody;

    final Handler myHandler = new Handler();

    private String htmlBody;

    private String htmlPic;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        htmlPic  = "http://www.baidu.com/img/baidu_sylogo1.gif";
        htmlBody = getString(R.string.test).toString();
        final JavaScriptInterface myJavaScriptInterface
        = new JavaScriptInterface(this); 
        
        wvDetailBody = (WebView) findViewById(R.id.detail_body);
//        wvDetailBody.getSettings().setLightTouchEnabled(true);
        wvDetailBody.getSettings().setJavaScriptEnabled(true);
//        wvDetailBody.getSettings().setPluginState(PluginState.ON);
        wvDetailBody.addJavascriptInterface(myJavaScriptInterface
          , "MyContent");    
//        wvDetailBody.setWebViewClient(new MyWebViewClient());
        wvDetailBody.loadUrl("file:///android_asset/www/show_detail.html");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    
    
    public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }
        
        public void showContacts() {
            // 封装成json传入�??js中处�?        	 JSONObject jsonData = new JSONObject(); 
             final JSONArray jsonArry = new JSONArray();
            
            try {
            	JSONObject jsonData = new JSONObject();
                jsonData.put("pic", htmlPic);
                jsonData.put("content", htmlBody);
                jsonArry.put(jsonData);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            myHandler.post(new Runnable() {
                @Override
                public void run() {
                    // This gets executed on the UI thread so it can safely modify Views
                    wvDetailBody.loadUrl("javascript:show('" + jsonArry + "')");
                }
            });
        }
    }
    
    
    private static  class MyWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading( WebView view, String url )
        {
            return false;
        }
    }
}
